package com.boarsoft.message.test.rocketmq;

import com.boarsoft.message.bean.Message;
import com.boarsoft.message.rocketmq.converter.RocketMsgConverter;
import com.boarsoft.serialize.JavaSerializer;
import com.boarsoft.serialize.ObjectSerializer;

public class DemoRocketMsgConverter implements RocketMsgConverter {
	/** 通过XML注入序列化器 */
	protected ObjectSerializer serializer = new JavaSerializer();

	@Override
	public Message convert(org.apache.rocketmq.common.message.Message rm) throws Exception {
		Message m = new Message();
		m.setContent(serializer.deserialize(rm.getBody()));
		// rm.getBuyerId()
		// rm.getDelayTimeLevel()
		// rm.getFlag()
		// rm.getProperties()
		m.setKey(rm.getKeys());
		// rm.getProperty(name)
		// m.setKey(rm.getTags()); //暂不支持RocketMQ的tags
		m.setTarget(rm.getTopic());
		// rm.getUserProperty(name);
		return m;
	}

	@Override
	public org.apache.rocketmq.common.message.Message convert(Message m) throws Exception {
		return new org.apache.rocketmq.common.message.Message(//
				m.getTarget(), m.getKey(), serializer.serialize(m.getContent()));
	}

	public ObjectSerializer getSerializer() {
		return serializer;
	}

	public void setSerializer(ObjectSerializer serializer) {
		this.serializer = serializer;
	}
}
